package com.onpositive.dataminer.rcp.views;

import java.util.ArrayList;

import com.onpositive.dataminer.core.AuditManager;
import com.onpositive.dataminer.core.AuditManager.ObjEntry;
import com.onpositive.dataminer.core.DefaultUniverse;
import com.onpositive.knowledge.model.IThing;
import com.onpositive.knowledge.model.values.IValueTransformer;
import com.onpositive.semantic.model.api.property.IProperty;
import com.onpositive.semantic.model.api.realm.IRealm;
import com.onpositive.semantic.model.binding.Binding;
import com.onpositive.semantic.ui.workbench.elements.SelectionBinding;
import com.onpositive.semantic.ui.workbench.elements.XMLView;

public class AuditInspectionView extends XMLView {

	private SelectionBinding m;

	public AuditInspectionView() {
		super("/ui/auditInspectionView.dlf");
		setSelectionProvider("valTab");
	}

	@Override
	public void dispose() {
		m.dispose();
		super.dispose();
	}

	public void createPartControl(org.eclipse.swt.widgets.Composite parent) {
		
		m = new SelectionBinding(getViewSite()) {
			
			public void setValue(Object value) {
				
				if( value == null )
					return ;
				
				if ( value instanceof IProperty ) {
					super.setValue(value);
					selectedProp = (IProperty) value ;
					Binding bnd = AuditInspectionView.this.getBinding("bnd");
					bnd.refresh() ;
					return ;
				}				
				
				if ( value instanceof AuditManager.ObjEntry<?> ){
					final ObjEntry<?> objEntry = (AuditManager.ObjEntry<?>)value;
					Object obj = objEntry.getObject() ;
					if( !(obj instanceof IValueTransformer) )
						return ;
					
					selectedTransformerId = objEntry.getId() ;
					Binding bnd = AuditInspectionView.this.getBinding("bnd");
					bnd.refresh() ;
					return ;
				}
			};
		};
		m.setId("selection");
		getBinding().addChild(m);
		super.createPartControl(parent);
	};

	IRealm<IThing> getAllObjects() {
		return DefaultUniverse.getInstance().all();
	}
	
	private IProperty selectedProp = null ;
	private String selectedTransformerId = null ;
	
	public ArrayList<AuditManager.InspectionDeltaUnit> getInspectionDelta()
	{
		if( selectedProp == null || selectedTransformerId == null )
			return null;
		
		AuditManager auditManager = (AuditManager) DefaultUniverse.getInstance().getAuditManager();
		return auditManager.getDelta(selectedTransformerId, selectedProp) ;		
	}

}
